/*
 * VideoSourcePanelTape.java
 *
 * Created on March 19, 2007, 1:53 PM
 */

package vars.annotation.ui.dialogs;

import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Collection;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;
import org.mbari.awt.event.NonDigitConsumingKeyListener;
import org.mbari.text.IgnoreCaseToStringComparator;

import vars.annotation.AnnotationDAOFactory;
import vars.annotation.ui.actions.OpenVideoArchiveUsingParamsAction;
import vars.annotation.ui.VARSProperties;

/**
 *
 * @author  brian
 */
public class VideoSourcePanelTape extends javax.swing.JPanel implements IVideoSourcePanel {
    
    public static final String PROP_VALID_VIDEO_SOURCE = "validVideoSource";
    
    private OpenVideoArchiveUsingParamsAction openAction;
    
    /** Creates new form VideoSourcePanelTape */
    public VideoSourcePanelTape(AnnotationDAOFactory annotationDAOFactory) {
        openAction = new OpenVideoArchiveUsingParamsAction(annotationDAOFactory);
        initComponents();
        postInit();
    }
    
    private void postInit() {
        diveNumberTextField.addKeyListener(new LocalKeyAdapter());
        tapeNumberTextField.addKeyListener(new LocalKeyAdapter());
        hdCheckBox.setSelected(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        cameraPlatformComboBox = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        diveNumberTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tapeNumberTextField = new javax.swing.JTextField();
        hdCheckBox = new javax.swing.JCheckBox();

        jLabel1.setText("Camera Platform:");

        cameraPlatformComboBox.setModel(new DefaultComboBoxModel(listCameraPlatforms()));

        jLabel2.setText("Dive Number:");

        jLabel3.setText("Tape Number:");

        hdCheckBox.setText("Check if High Definition");
        hdCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        hdCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        layout.setHorizontalGroup(
        	layout.createParallelGroup(GroupLayout.LEADING)
	        	.add(layout.createSequentialGroup()
		        	.addContainerGap()
		        	.add(layout.createParallelGroup(GroupLayout.LEADING)
			        	.add(layout.createSequentialGroup()
				        	.add(layout.createParallelGroup(GroupLayout.LEADING)
					        	.add(jLabel1)
					        	.add(jLabel2)
					        	.add(jLabel3))
				        	.addPreferredGap(LayoutStyle.RELATED)
				        	.add(layout.createParallelGroup(GroupLayout.LEADING)
					        	.add(cameraPlatformComboBox, 0, 342, Short.MAX_VALUE)
					        	.add(diveNumberTextField, GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
					        	.add(tapeNumberTextField, GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)))
			        	.add(layout.createSequentialGroup()
				        	.add(157, 157, 157)
				        	.add(hdCheckBox)))
		        	.addContainerGap())
        );
        layout.setVerticalGroup(
        	layout.createParallelGroup(GroupLayout.LEADING)
	        	.add(layout.createSequentialGroup()
		        	.addContainerGap()
		        	.add(layout.createParallelGroup(GroupLayout.BASELINE)
			        	.add(jLabel1)
			        	.add(cameraPlatformComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
		        	.addPreferredGap(LayoutStyle.RELATED)
		        	.add(layout.createParallelGroup(GroupLayout.BASELINE)
			        	.add(jLabel2)
			        	.add(diveNumberTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
		        	.addPreferredGap(LayoutStyle.RELATED)
		        	.add(layout.createParallelGroup(GroupLayout.BASELINE)
			        	.add(jLabel3)
			        	.add(tapeNumberTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
		        	.addPreferredGap(LayoutStyle.RELATED)
		        	.add(hdCheckBox)
		        	.addContainerGap(19, Short.MAX_VALUE))
        );
        this.setLayout(layout);
    }

    public void open() {
        validateVideoSource();
        if (isValidVideoSource()) {
            int seqNumber = Integer.parseInt(diveNumberTextField.getText());
            String platform = (String) cameraPlatformComboBox.getSelectedItem();
            int tapeNumber = Integer.parseInt(tapeNumberTextField.getText());
            openAction.setPlatform(platform);
            openAction.setSeqNumber(seqNumber);
            openAction.setTapeNumber(tapeNumber);
            final String postfix = hdCheckBox.isSelected() ? "HD" : null;
            openAction.setPostfix(postfix);
            openAction.doAction();
        }
        else {
            // TODO What should we do if the video source is not valid?
        }
    }
    

    private String[] listCameraPlatforms() {
        final Collection cameraPlatforms = VARSProperties.getCameraPlatforms();
        String[] cp = new String[cameraPlatforms.size()];
        cameraPlatforms.toArray(cp);
        Arrays.sort(cp, new IgnoreCaseToStringComparator());
        return cp;
    }

    public boolean isValidVideoSource() {
        return true;
    }
    
    private void validateVideoSource() {
    }
    
    public JCheckBox getHdCheckBox() {
        return hdCheckBox;
    }
    
    
    private javax.swing.JComboBox cameraPlatformComboBox;
    private javax.swing.JTextField diveNumberTextField;
    private javax.swing.JCheckBox hdCheckBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField tapeNumberTextField;
    
    /**
     * KeyAdapter that only allows entry of digit characters. Revalidates 
     * video source on keystroke.
     */
    class LocalKeyAdapter extends NonDigitConsumingKeyListener {
        @Override
        public void keyTyped(KeyEvent e) {
            super.keyTyped(e);
            validateVideoSource();
        }
    }
}
